/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package functions;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.Database;

/**
 *
 * @author JArik
 */
public class Visits {


    public static int addVisit(int dogId, int doc, Date date, double visitPrice, double other,boolean byCard, int healingId) throws SQLException
    {
        Database db = new Database();
        if(date != null)
        date = new Date(date.getTime());
        int res=0;
        String what="", values="";
        what =  "dogId,exec,date,visitPrice,otherCosts,healingId";
        values = dogId+","+doc+",'"+date+"',"+visitPrice+","+other+","+healingId;
        if(byCard)
        {
            what +=", cardPayment";
            values +=",1";
        }
        ResultSet set = db.insertDBInfo("visits", what, values );
        if(set.next())
            res = set.getInt(1);
        db.closeStatement();
        db = null;
        return res;
    }
    public static int addVisit(int dogId, int doc, Date date, double visitPrice,
                               double other, double visitPricePayed, double visitOtherPayed,
                               boolean byCard,
                               String currency,
                               String descr, String recom,
                               String status,int healingId) throws SQLException
    {
        Database db = new Database();
        
        int res=0;
        if(dogId <=0)
            throw new SQLException("Wrong dog");
        if(doc <=0)
            throw new SQLException("Wrong doctor");
        if(date == null)
            throw new SQLException("Wrong date");
        if(visitPrice < 0)
            throw new SQLException("Wrong visit price");
        if(other < 0)
            throw new SQLException("Wrong other visit price: "+other);
        if(healingId <=0)
            throw new SQLException("Wrong healing ID");
        if(currency == null || currency.isEmpty())
            throw new SQLException("Wrong currency");
        
        date = new Date(date.getTime());

        String what="", values="";
        what =  "dogId,exec,date,visitPrice,otherCosts,descr,recom,status,healingId,visitPricePayed, visitOtherPayed,currency";
        values = dogId+","+doc+",'"+date+"',"+visitPrice+","+other+",'"+descr+
                 "','"+recom+"','"+status+"',"+healingId+","+visitPricePayed+","+visitOtherPayed+",'"+currency+"'";
        if(byCard)
        {
            what +=", cardPayment";
            values +=",1";
        }
        ResultSet set = db.insertDBInfo("visits",
                what,
                values);
        if(set.next())
            res= set.getInt(1);
        db.closeStatement();
        db = null;
        return res;        
    }

    public static int changeVisit(int visitId, int dogId, int doc,
                               Date date, double visitPrice,
                               double other, double visitPricePayed, double visitOtherPayed,
                               boolean byCard,
                               String currency,
                               String descr, String recom,
                               String status,int healingId) throws SQLException
    {
        Database db = new Database();
        
        if(visitId <= 0)
            throw new SQLException("Неправельный номер визита: "+visitId);
        if(dogId <=0)
            throw new SQLException("Wrong dog");
        if(doc <=0)
            throw new SQLException("Wrong doctor");
        if(date == null)
            throw new SQLException("Wrong date");
        if(visitPrice < 0)
            throw new SQLException("Wrong visit price");
        if(other < 0)
            throw new SQLException("Wrong other visit price");
        if(healingId <=0)
            throw new SQLException("Wrong healing ID");
        if(currency == null || currency.isEmpty())
            throw new SQLException("Wrong currency");
        
        int cardP=0;
        if(byCard)
            cardP=1;
        date = new Date(date.getTime());
        int set = db.updateDBInfo("visits",
                "dogId="+dogId+"," +
                "exec="+doc+","+
                "date='"+date+"',"+
                "visitPrice="+visitPrice+","+
                "otherCosts="+other+","+
                "descr='"+descr+"',"+
                "recom='"+recom+"',"+
                "status='"+status+"',"+
                "healingId="+healingId+","+
                "visitPricePayed="+visitPricePayed+","+
                "visitOtherPayed="+visitOtherPayed+","+
                "currency='"+currency+"',"+
                "cardPayment="+cardP,
                " id="+visitId);
        db.closeStatement();
        db = null;
        return set;

    }

    //for installation
    public static int addIgnoreVisit(int dogId, int doc, Date date, double visitPrice,
                               double other, double visitPricePayed, double visitOtherPayed,
                               String descr, String recom,
                               String status,int healingId) throws SQLException
    {
        Database db = new Database();
        
        int res=0;
        if(dogId <=0)
            throw new SQLException("Wrong dog");
        if(doc <=0)
            throw new SQLException("Wrong doctor");
        if(date == null)
            throw new SQLException("Wrong date");
       // if(visitPrice < 0)
       //     visitPrice = 0;
       // if(other < 0)
       //     other = 0;
        if(healingId <=0)
            throw new SQLException("Wrong healing ID");

        date = new Date(date.getTime());
        ResultSet set = db.insertDBInfo("visits",
                "dogId,exec,date,visitPrice,otherCosts," +
                "descr,recom,status,healingId,visitPricePayed, visitOtherPayed",
                 dogId+","+doc+",'"+date+"',"+visitPrice+","+other+",'"+descr+
                 "','"+recom+"','"+status+"',"+healingId+","+visitPricePayed+","+visitOtherPayed);
        if(set.next())
            res= set.getInt(1);
        db.closeStatement();
        db = null;
        return res;
    }

    public static void deleteVisit(int visitId) throws SQLException {
        Database db = new Database();
        db.deleteDBInfo("visits", "id="+visitId);
        db.deleteDBInfo("diagnoses_made", "visitId="+visitId);
        db.deleteDBInfo("operations_made", "visitId="+visitId);
        db.deleteDBInfo("discharged_drugs", "visitId="+visitId);

        db.closeStatement();
        db = null;
    }
}
